﻿namespace Reviewer.Data
{
    using System;
    using System.Reflection;
    using IQToolkit;
    using IQToolkit.Data;
    using IQToolkit.Data.Mapping;
    using Reviewer.Entities;

    [CLSCompliant(false)]
    public sealed class ReviewerDBContext
    {
        public ReviewerDBContext()
        {
            this.EntityProvider = new IQToolkit.Data.MySqlClient.MySqlQueryProvider(
               new MySql.Data.MySqlClient.MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Reviewer.Data"].ConnectionString),
               new IQToolkit.Data.Mapping.AttributeMapping(typeof(ReviewerDBContext)),
               IQToolkit.Data.Common.QueryPolicy.Default);

        }

        public IEntityProvider EntityProvider
        {
            get;
            set;
        }

        /// <summary>
        /// Gets the metadata table.
        /// </summary>
        /// <typeparam name="T">Metadata table type</typeparam>
        /// <returns>Entity table for the metadata table</returns>
        public IEntityTable<T> GetMetadataTable<T>() where T : class,IMetadataType
        {
            PropertyInfo[] properties = this.GetType().GetProperties();
            foreach (PropertyInfo propertyInfo in properties)
            {
                if (propertyInfo.PropertyType.Equals(typeof(IEntityTable<T>)))
                {
                    return propertyInfo.GetValue(this) as IEntityTable<T>;
                }
            }

            return null;
        }


        [Table(Name = "membersattribute", Alias = "MA")]
        [Column(Member = "MembersId", Name = "MembersId", IsPrimaryKey = true, IsGenerated = false)]
        [Column(Member = "Key", Name = "KeyName", IsPrimaryKey = true, IsGenerated = false)]
        [Column(Member = "Value", Name = "DataValue")]
        public IEntityTable<MembersAttribute> MembersAttribute
        {
            get { return (IEntityTable<MembersAttribute>)EntityProvider.GetTable(typeof(MembersAttribute), "MembersAttribute"); }
        }

        [Table(Name = "members", Alias = "M")]
        [Column(Member = "Id", Name = "Id", IsPrimaryKey = true, IsGenerated = false)]
        [Column(Member = "UserName", Name = "Username")]
        public IEntityTable<Members> Members
        {
            get
            {
                return (IEntityTable<Members>)EntityProvider.GetTable(typeof(Members), "Members");
            }
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "PEGI")]
        [Table(Name = "PEGIRating", Alias = "PegiRating")]
        [Column(Member = "GameId", Name = "GameUid", IsPrimaryKey = true, IsGenerated = false)]
        [Column(Member = "Format", Name = "Format", IsPrimaryKey = true, IsGenerated = false)]
        [Column(Member = "AgeRating", Name = "Age")]
        [Column(Member = "IsRatedBadLanguage", Name = "Language")]
        [Column(Member = "IsRatedDiscrimination", Name = "Discrimination")]
        [Column(Member = "IsRatedDrugs", Name = "Drugs")]
        [Column(Member = "IsRatedFear", Name = "Fear")]
        [Column(Member = "IsRatedGambling", Name = "Gambling")]
        [Column(Member = "IsRatedSex", Name = "Sex")]
        [Column(Member = "IsRatedViolence", Name = "Violence")]
        [Column(Member = "IsRatedOnline", Name = "Online")]
        public IEntityTable<PEGIRating> PEGIRating
        {
            get
            {
                return (IEntityTable<PEGIRating>)EntityProvider.GetTable(typeof(PEGIRating), "PEGIRating");
            }
        }

        [Table(Name = "UserProfileProperty", Alias = "UserProfileProperty")]
        [Column(Name = "ProfileId", Alias = "ProfileId", IsPrimaryKey = true, IsGenerated = false)]
        [Column(Name = "Name", Alias = "PropertyName", IsPrimaryKey = true, IsGenerated = false)]
        [Column(Name = "Value", Alias = "PropertyValue")]
        public IEntityTable<UserProfileProperty> UserProfileProperty
        {
            get
            {
                return (IEntityTable<UserProfileProperty>)EntityProvider.GetTable(typeof(UserProfileProperty), "UserProfileProperty");
            }
        }

        [Table(Name = "GameAccessory", Alias = "GameAccessory")]
        [Column(Name = "Id", Member = "Id", IsPrimaryKey = true, IsGenerated = true, IsComputed = true)]
        [Column(Name = "Name", Member = "Name")]
        [Column(Name = "FormatId", Member = "FormatId")]
        [Column(Name = "IconFileName", Member = "IconFileName")]
        public IEntityTable<GameAccessory> GameAccessory
        {
            get
            {
                return (IEntityTable<GameAccessory>)EntityProvider.GetTable(typeof(GameAccessory), "GameAccessory");
            }
        }

        [Table(Name = "V2_GloFormat", Alias = "Format")]
        [Column(Name = "ID", Member = "Id", IsPrimaryKey = true, IsGenerated = true)]
        [Column(Name = "Navn", Member = "Name")]
        public IEntityTable<Format> Format
        {
            get
            {
                return (IEntityTable<Format>)EntityProvider.GetTable(typeof(Format), "Format");
            }
        }

        [Table(Name = "V2_GloGenres", Alias = "Genre")]
        [Column(Name = "ID", Member = "Id", IsPrimaryKey = true, IsGenerated = true)]
        [Column(Name = "Navn", Member = "Name")]
        public IEntityTable<Genre> Genre
        {
            get
            {
                return (IEntityTable<Genre>)EntityProvider.GetTable(typeof(Genre), "Genre");
            }
        }

        [Table(Name = "MultiPlayerInfo", Alias = "MultiPlayerInfo")]
        [Column(Name = "Id", Member = "Id", IsPrimaryKey = true, IsGenerated = true)]
        [Column(Name = "Name", Member = "Name")]
        public IEntityTable<MultiplayerInfo> MultiPlayerInfo
        {
            get
            {
                return (IEntityTable<MultiplayerInfo>)EntityProvider.GetTable(typeof(MultiplayerInfo), "MultiPlayerInfo");
            }
        }

        [Table(Name = "NewsType", Alias = "NewsType")]
        [Column(Name = "Id", Member = "Id", IsPrimaryKey = true, IsGenerated = true)]
        [Column(Name = "Name", Member = "Name")]
        public IEntityTable<NewsType> NewsType
        {
            get
            {
                return (IEntityTable<NewsType>)EntityProvider.GetTable(typeof(NewsType), "NewsType");
            }
        }

        [Table(Name = "InfoPage", Alias = "InfoPage")]
        [Column(Name = "Id", Member = "Id", IsPrimaryKey = true, IsGenerated = true)]
        [Column(Name = "PageName", Member = "PageName")]
        [Column(Name = "PageData", Member = "PageData")]
        public IEntityTable<InfoPage> InfoPage
        {
            get
            {
                return (IEntityTable<InfoPage>)EntityProvider.GetTable(typeof(InfoPage), "InfoPage");
            }
        }

        [Table(Name = "GameFactsheet", Alias = "GameFactsheet")]
        [Column(Name = "GameId", Member = "GameId", IsPrimaryKey = true, IsGenerated = false)]
        [Column(Name = "Data", Member = "Data")]
        public IEntityTable<GameFactsheet> GameFactsheet
        {
            get
            {
                return (IEntityTable<GameFactsheet>)EntityProvider.GetTable(typeof(GameFactsheet), "GameFactsheet");
            }
        }
    }
}
